package com.example.core.lock;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;


/***
 *
 * 基于Redis实现的分布式锁
 */
public class RedisLock implements Lock {

    private RedissonClient redissonClient;

    private RLock rLock;

    private String lockKey;

    public RedissonClient getRedissonClient() {
        return redissonClient;
    }

    public void setRedissonClient(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    public RLock getrLock() {
        return rLock;
    }

    public void setrLock(RLock rLock) {
        this.rLock = rLock;
    }

    public String getLockKey() {
        return lockKey;
    }

    public void setLockKey(String lockKey) {
        this.lockKey = lockKey;
    }

    public RedisLock(RedissonClient redissonClient, String lockKey) {
        this.redissonClient = redissonClient;
        this.lockKey = lockKey;
    }

    @Override
    public void lock() throws Exception {
        this.rLock = redissonClient.getLock(lockKey);
        this.rLock.lock();
    }

    @Override
    public void unlock() {
        this.rLock.unlock();
    }
}
